//
//  NewFutureCollectionViewController.swift
//  WeiBo_One
//
//  Created by 李炎 on 15/10/25.
//  Copyright © 2015年 蜡笔小炎. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "NewFutureCollectionViewCell"
private let newFutureImageCount = 4

class NewFutureCollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
 
        collectionView?.backgroundColor = UIColor.whiteColor()
        collectionView?.registerClass(NewFutureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    init() {
        // 创建并设置布局
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        // 设置布局。只有调用了父类指定构造方法，下面属性才能被创建
        super.init(collectionViewLayout: layout)
        // 设置属性
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 隐藏状态栏
    override func prefersStatusBarHidden() -> Bool { return true }
}

//MARK: - 数据源方法
extension NewFutureCollectionViewController {
    // 返回多少 item
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return newFutureImageCount
    }
    // 返回 item
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFutureCollectionViewCell
        cell.imageIndex = indexPath.item
        return cell
    }
}

// MARK: - 代理方法
extension NewFutureCollectionViewController {

    // 并非 scrollViewDidEndScrollingAnimation 代理方法
    // 当scrollView 停止滚动时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let count = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        
        if count != newFutureImageCount - 1 {
            return
        }
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: count, inSection: 0)) as! NewFutureCollectionViewCell
        
        cell.startBtn.hidden = false
    }
    
    
    
}

//MARK: - 自定义 cell 类
class NewFutureCollectionViewCell: UICollectionViewCell {
    
    var imageIndex: Int = 0 {
        didSet {
            backImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startBtn.hidden = true
        }
    }
    //图片
    lazy var backImageView: UIImageView = UIImageView()
    //“开始体验”按钮
    lazy var startBtn: UIButton = UIButton(title: "开始体验",color: UIColor.whiteColor(), fontSize: 14, backImageName: "new_feature_finish_button")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
        
        // 添加子视图
        addSubview(backImageView)
        addSubview(startBtn)
        
        //设置子视图的位置
        backImageView.frame = bounds
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
        startBtn.addTarget(self, action: "clickStartBtn", forControlEvents: .TouchUpInside)
    }
    
    //MARK: - 点击事件
    @objc private func clickStartBtn() {
        // 跳转主界面
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootControllerNotification, object: nil)
    }
}









